import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:todo_app/components/FontIcon.dart';
import 'package:todo_app/components/TextExt.dart';
import 'package:todo_app/constants/DColor.dart';
import 'package:todo_app/constants/Screen.dart';
import 'package:todo_app/hooks/Throttle.dart';
import 'package:todo_app/layout/Gaps.dart';
import 'package:todo_app/utils/ExtHelper.dart';

class MessageTips extends StatelessWidget {
  final String tips;
  
  const MessageTips({ super.key, required this.tips });

  @override
  Widget build(BuildContext context) {
    return TextExt.desc(tips);
  }
}

class MessageSection extends StatelessWidget {
  final DateTime dateTime;
  final EdgeInsetsGeometry? margin;
  
  const MessageSection({super.key, required this.dateTime, this.margin });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin ?? EdgeInsets.symmetric(vertical: 10.dp),
      alignment: Alignment.center,
      child: TextExt.desc(dateTime.isToday() ? dateTime.toHm() : dateTime.toYMDHm())
    );
  }
}

class MessageBubble extends HookWidget {
  const MessageBubble({
    super.key, 
    this.isMe = false, 
    this.showRedPoint = false, 
    this.videoType = false,
    this.padding, 
    this.borderRadius, 
    this.icon, 
    this.margin, 
    this.onPress, 
    this.content, 
    this.onLongPress, 
    this.hasImg = false, 
    this.hasError = false, 
    this.onSendAgain, 
    this.isSending = false 
  });

  final bool isMe;
  final Widget? icon;
  final Widget? content;
  final VoidCallback? onLongPress;
  final bool hasImg;
  final bool hasError;
  final VoidCallback? onSendAgain;
  final bool isSending;
  final EdgeInsetsGeometry? margin;
  final BorderRadius? borderRadius;
  final VoidCallback? onPress;
  final EdgeInsetsGeometry? padding;
  final bool videoType;
  final bool showRedPoint;

  @override
  Widget build(BuildContext context) {
    final double iconWidth = 32.dp;
    var iconWrap = icon == null ? null : Container(
      margin: EdgeInsets.symmetric(horizontal: 10.dp),
      width: iconWidth,
      height: iconWidth,
      child: icon,
    );
    var contentWrap = Container(
      decoration: BoxDecoration(
        borderRadius: borderRadius ?? BorderRadius.circular(4.dp),
        color: isMe == true ? DColor.second.withOpacity(0.3) : Colors.white,
      ),
      margin: margin,
      padding: EdgeInsets.symmetric(horizontal: 12.dp, vertical: 8.dp),
      child: content,
    );

    var sendAgain = useThrottle((value) { 
      if(onSendAgain != null) onSendAgain!();
    }, initialValue: 0, duration: const Duration(milliseconds: 400));

    List<Widget?> rowContent = <Widget?>[ iconWrap, contentWrap ];
    if(hasError == true) {
      rowContent.add(
        Container(
          alignment: Alignment.center,
          height: 42.dp,
          child: InkWell(
            onTap: () {
              sendAgain.value++;
            },
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 4.dp),
              width: 16.dp,
              height: 16.dp,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8.dp)
              ),
              child: FontIcon(icon: FontIcons.info, color: Colors.white, size: 12.dp,)
            ),
          )
        ),
      );
    }
    if(isSending == true) {
       rowContent.add(
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 4.dp),
          child: CupertinoActivityIndicator(animating: true, radius: 8.dp),
        ),
      );
    }

    if(showRedPoint == true) {
      rowContent.add(
        Container(
          alignment: Alignment.center,
          margin: isMe == true ? EdgeInsets.only(right: 4.dp) :  EdgeInsets.only(left: 4.dp),
          child: Container(
            width: 8,
            height: 8,
            decoration: BoxDecoration(
              color: Colors.red,
              borderRadius: BorderRadius.circular(4)
            )
          )
        )
      );
    }


    if(isMe == true) {
      rowContent = rowContent.reversed.toList();
    }
    
    return SizedBox(
      width: Screen.width,
      child: GestureDetector(
        onLongPress: onLongPress,
        onTap: onPress,
        child: Container(
          alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
          margin: EdgeInsets.only(bottom: 8.dp),
          child: Row(
            mainAxisAlignment: isMe ? MainAxisAlignment.end : MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: rowContent.where((item)=> item != null).map<Widget>((e) => e!).toList(),
          ),
        )
      )
    );
  }
}